<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="项目编号" prop="xiangmubianhao">
          <el-input
            v-model="queryParams.xiangmubianhao"
            placeholder="请输入项目编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="项目名称" prop="xiangmumingcheng">
          <el-input
            v-model="queryParams.xiangmumingcheng"
            placeholder="请输入项目名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="科研类别" prop="keyanleibie">
          <el-input
            v-model="queryParams.keyanleibie"
            placeholder="请输入科研类别"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:keyanxiangmu:add']"
          >新增项目</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:keyanxiangmu:edit']"
          >修改</el-button>
        </el-col> -->
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:keyanxiangmu:remove']"
          >删除</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:keyanxiangmu:export']"
          >导出</el-button>
        </el-col> -->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>


<!-- 项目成果对话框*********************************************************************************************************** -->
      <el-dialog :title="title" :visible.sync="chenguoopen" width="500px" append-to-body>
        <el-form ref="chengguoform" :model="chengguoform" :rules="chengguorules" label-width="80px">
          <el-form-item label="项目名称" prop="xiangmumingcheng">
            <el-input v-model="chengguoform.xiangmumingcheng" placeholder="请输入项目名称" readonly="true"/>
          </el-form-item>
          <!-- <el-form-item label="项目图片" prop="xiangmutupian">
            <el-input v-model="chengguoform.xiangmutupian" type="textarea" placeholder="请输入内容" />
          </el-form-item> -->
          <!-- <el-form-item label="科研类别" prop="keyanleibie">
            <el-input v-model="chengguoform.keyanleibie" placeholder="请输入科研类别" />
          </el-form-item> -->
          <el-form-item label="研究成果" prop="yanjiuchengguo">
            <el-input v-model="chengguoform.yanjiuchengguo" type="textarea" placeholder="请输入内容" />
          </el-form-item>
          <!-- <el-form-item label="成果数量" prop="chengguoshuliang">
            <el-input v-model="chengguoform.chengguoshuliang" placeholder="请输入成果数量" />
          </el-form-item> -->
          <!-- <el-form-item label="结项时间" prop="jiexiangshijian">
            <el-date-picker clearable
              v-model="chengguoform.jiexiangshijian"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择结项时间">
            </el-date-picker>
          </el-form-item> -->
          <!-- <el-form-item label="学号" prop="xuehao">
            <el-input v-model="chengguoform.xuehao" placeholder="请输入学号" />
          </el-form-item>
          <el-form-item label="学生姓名" prop="xueshengxingming">
            <el-input v-model="chengguoform.xueshengxingming" placeholder="请输入学生姓名" />
          </el-form-item>
          <el-form-item label="专业" prop="zhuanye">
            <el-input v-model="chengguoform.zhuanye" placeholder="请输入专业" />
          </el-form-item> -->
          <!-- <el-form-item label="科研账号" prop="keyanzhanghao">
            <el-input v-model="chengguoform.keyanzhanghao" placeholder="请输入科研账号" />
          </el-form-item>
          <el-form-item label="负责人" prop="fuzeren">
            <el-input v-model="chengguoform.fuzeren" placeholder="请输入负责人" />
          </el-form-item> -->
          <!-- <el-form-item label="是否审核" prop="sfsh">
            <el-input v-model="form.sfsh" placeholder="请输入是否审核" />
          </el-form-item>
          <el-form-item label="审核回复" prop="shhf">
            <el-input v-model="form.shhf" type="textarea" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="赞" prop="thumbsupnum">
            <el-input v-model="form.thumbsupnum" placeholder="请输入赞" />
          </el-form-item>
          <el-form-item label="踩" prop="crazilynum">
            <el-input v-model="form.crazilynum" placeholder="请输入踩" />
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitchengguoForm">确 定</el-button>
          <el-button @click="chenguocancel">取 消</el-button>
        </div>
      </el-dialog>
      <!-- 项目成果对话框结束************************************************************************************* -->



      <el-table v-loading="loading" :data="keyanxiangmuList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column label="主键" align="center" prop="id" /> -->
        <el-table-column label="项目编号" align="center" prop="xiangmubianhao" />
        <el-table-column label="项目名称" align="center" prop="xiangmumingcheng" />
        <!-- <el-table-column label="项目图片" align="center" prop="xiangmutupian" /> -->
        <el-table-column label="科研类别" align="center" prop="keyanleibie" />
        <!-- <el-table-column label="科研预算" align="center" prop="keyanyusuan" />
        <el-table-column label="研究目的" align="center" prop="yanjiumude" />
        <el-table-column label="科研文件" align="center" prop="keyanwenjian" />
        <el-table-column label="研究内容" align="center" prop="yanjiuneirong" /> -->
        <!-- <el-table-column label="申请日期" align="center" prop="shenqingriqi" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.shenqingriqi, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column> -->
        <el-table-column label="科研账号" align="center" prop="keyanzhanghao" />
        <el-table-column label="负责人" align="center" prop="fuzeren" />
        <el-table-column label="联系电话" align="center" prop="lianxidianhua" />
        <el-table-column label="项目进度" align="center" prop="sfsh" />
        <!-- <el-table-column label="审核回复" align="center" prop="shhf" /> -->
        <!-- <el-table-column label="赞" align="center" prop="thumbsupnum" />
        <el-table-column label="踩" align="center" prop="crazilynum" /> -->
        <!-- <el-table-column label="最近点击时间" align="center" prop="clicktime" width="180"> -->
          <!-- <template slot-scope="scope">
            <span>{{ parseTime(scope.row.clicktime, '{y}-{m}-{d}') }}</span>
          </template> -->
        <!-- </el-table-column> -->
        <!-- <el-table-column label="点击次数" align="center" prop="clicknum" /> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-plus"
              @click="addxiangmuchengguo(scope.row)"
            >增加项目成果</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:keyanxiangmu:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <el-pagination
      v-show="total>0"
        background
        :total="this.total"
        :page-size="this.queryParams.limit"
        @current-change="handleCurrentChange">
      </el-pagination>
  
      <!-- 添加或修改科研项目对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @close="handleDialogClose">
        <el-form ref="formRef" :model="form" :rules="rules" label-width="110px">
          <!-- <el-form-item label="项目编号" prop="xiangmubianhao">
            <el-input v-model="form.xiangmubianhao" placeholder="请输入项目编号" />
          </el-form-item> -->
          <el-form-item label="项目名称" prop="xiangmumingcheng">
            <el-input v-model="form.xiangmumingcheng" placeholder="请输入项目名称" />
          </el-form-item>
          <!-- <el-form-item label="项目图片" prop="xiangmutupian">
            <el-input v-model="form.xiangmutupian" type="textarea" placeholder="请输入内容" />
          </el-form-item> -->
          <!-- <el-form-item label="科研类别" prop="keyanleibie">
            <el-input v-model="form.keyanleibie" placeholder="请输入科研类别" />
          </el-form-item> -->
          <el-form-item label="科研类别" prop="keyanleibie">
            <el-select v-model="form.keyanleibie" placeholder="请选择科研类别">
              <el-option
                v-for="item in keyanleibieList"
                :key="item.id"
                :label="item.keyanleibie"
                :value="item.keyanleibie"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="科研预算" prop="keyanyusuan">
            <el-input v-model="form.keyanyusuan" placeholder="请输入科研预算" />
          </el-form-item>
          <el-form-item label="研究目的" prop="yanjiumude">
            <el-input v-model="form.yanjiumude" type="textarea" placeholder="请输入内容" />
          </el-form-item>
          <!-- <el-form-item label="科研文件" prop="keyanwenjian">
            <el-input v-model="form.keyanwenjian" type="textarea" placeholder="请输入内容" />
          </el-form-item> -->
          <el-form-item label="研究内容" prop="yanjiuneirong">
            <el-input v-model="form.yanjiuneirong" type="textarea" placeholder="请输入内容" />
          </el-form-item>
          <!-- <el-form-item label="申请日期" prop="shenqingriqi">
            <el-date-picker clearable
              v-model="form.shenqingriqi"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择申请日期">
            </el-date-picker>
          </el-form-item> -->
          <!-- <el-form-item label="科研账号" prop="keyanzhanghao">
            <el-input v-model="form.keyanzhanghao" placeholder="请输入科研账号" />
          </el-form-item> -->
          <el-form-item label="负责人" prop="fuzeren">
            <el-input v-model="form.fuzeren" placeholder="请输入负责人" />
          </el-form-item>
          <el-form-item label="联系电话" prop="lianxidianhua">
            <el-input v-model="form.lianxidianhua" placeholder="请输入联系电话" />
          </el-form-item>
          <el-form-item label="是否立即审核" prop="sfsh">
            <el-select v-model="form.sfsh" placeholder="请选择是否审核">
              <el-option label="是" value="申请中"></el-option>
              <el-option label="否" value="待审核"></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="审核回复" prop="shhf">
            <el-input v-model="form.shhf" type="textarea" placeholder="请输入内容" />
          </el-form-item> -->
          <!-- <el-form-item label="赞" prop="thumbsupnum">
            <el-input v-model="form.thumbsupnum" placeholder="请输入赞" />
          </el-form-item>
          <el-form-item label="踩" prop="crazilynum">
            <el-input v-model="form.crazilynum" placeholder="请输入踩" />
          </el-form-item>
          <el-form-item label="最近点击时间" prop="clicktime">
            <el-date-picker clearable
              v-model="form.clicktime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择最近点击时间">
            </el-date-picker>
          </el-form-item> -->
          <!-- <el-form-item label="点击次数" prop="clicknum">
            <el-input v-model="form.clicknum" placeholder="请输入点击次数" />
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
//   import { listKeyanxiangmu, getKeyanxiangmu, delKeyanxiangmu, addKeyanxiangmu, updateKeyanxiangmu } from "@/api/system/keyanxiangmu";
  
  export default {
    name: "Keyanxiangmu",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: false,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 科研项目表格数据
        keyanxiangmuList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 项目成果弹出层
        chenguoopen: false,
        // 科研类别列表
        keyanleibieList: [],
        // 查询参数
        queryParams: {
          page: 1,
          limit: 10,
          xiangmubianhao: null,
          xiangmumingcheng: null,
          keyanleibie: null,
          keyanzhanghao: null,
        },
        // 表单参数
        form: {},
        //成果表单
        chengguoform: {
          id: null,
          addtime: null,
          xiangmumingcheng: null,
          xiangmutupian: null,
          keyanleibie: null,
          yanjiuchengguo: null,
          chengguoshuliang: null,
          jiexiangshijian: null,
          xuehao: null,
          xueshengxingming: null,
          zhuanye: null,
          keyanzhanghao: null,
          fuzeren: null,
          sfsh: null,
          shhf: null,
          thumbsupnum: null,
          crazilynum: null
        },
        // 表单校验
        rules: {
          xiangmumingcheng: [
          { required: true, message: '项目名称不能为空', trigger: 'blur' }
        ],
        keyanleibie: [
          { required: true, message: '科研类别不能为空', trigger: 'change' }
        ],
        fuzeren:[
        { required: true, message: '负责人不能为空', trigger: 'blur' }
        ],
        keyanyusuan: [
          { required: true, message: '科研预算不能为空', trigger: 'blur' },
          {
    validator: (rule, value, callback) => {
      if (value === '' || isNaN(value)) {
        callback(new Error('科研预算必须为数字值'));
      } else {
        callback();
      }
    },
    trigger: 'blur'
  },
    { min: 0, message: '科研预算不能小于0', trigger: 'blur' },
        ],
        yanjiumude: [
          { required: true, message: '研究目的不能为空', trigger: 'blur' }
        ],
        yanjiuneirong: [
          { required: true, message: '研究内容不能为空', trigger: 'blur' }
        ],
        lianxidianhua: [
          { required: true, message: '联系电话不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的电话号码', trigger: 'blur' }
        ],
        sfsh: [
          { required: true, message: '是否立即审核不能为空', trigger: 'change' }
        ],
        }
      };
    },
    created() {
      this.getList();
      this.getkeyanleibieList();
    },
    methods: {
      //提交项目成果
      submitchengguoForm(){
        this.$http({
    url: `/xiangmuchengguo/add`,
    method: "post",
    data: this.chengguoform // 使用 params 而不是 data
}).then(() => {
         this.chenguoopen=false;
        
        }).catch(() => {});
      },
      //新增项目成果
      addxiangmuchengguo(row){
        this.chenguoopen = true;
        this.chengguoform.xiangmumingcheng=row.xiangmumingcheng;
        this.chengguoform.keyanleibie=row.keyanleibie;
        this.chengguoform.fuzeren=row.fuzeren;
        this.chengguoform.keyanzhanghao=row.keyanzhanghao;
      },
      handleDialogClose(){
        this.reset();
        this.$refs["formRef"].resetFields();
      },
      getkeyanleibieList() {
        this.$http({
    url: `/keyanleibie/list`,
    method: "get",
    params: this.queryParams // 使用 params 而不是 data
}).then(response => {
          this.keyanleibieList = response.data.data.list;
        });
      },
        handleCurrentChange(val){
        this.queryParams.page = val;
        this.getList();
      },
      /** 查询科研项目列表 */
      getList() {
        this.loading = true;
        const role = this.$storage.get('role');
        const adminName = this.$storage.get('adminName'); // 提前获取 adminName，避免重复调用

        if (role === '科研部门') {
            this.queryParams.keyanzhanghao = adminName;
        }
        this.$http({
    url: `/keyanxiangmu/list`,
    method: "get",
    params: this.queryParams // 使用 params 而不是 data
}).then(response => {
          this.keyanxiangmuList = response.data.data.list;
          this.total = response.data.data.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        // this.open = false;
        // this.reset();
        this.$nextTick(() => {
          this.$refs["formRef"].resetFields(); // 调用 resetFields 方法重置验证状态
        this.reset(); // 重置表单数据到初始状态，如果你有需要的话

        this.open = false;
      });
      },
      chenguocancel(){
        this.$nextTick(() => {
          this.$refs["chengguoform"].resetFields(); // 调用 resetFields 方法重置验证状态
        this.chenguoreset(); // 重置表单数据到初始状态，如果你有需要的话

        this.chenguoopen = false;
      });
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          addtime: null,
          xiangmubianhao: null,
          xiangmumingcheng: null,
          xiangmutupian: null,
          keyanleibie: null,
          keyanyusuan: null,
          yanjiumude: null,
          keyanwenjian: null,
          yanjiuneirong: null,
          shenqingriqi: null,
          keyanzhanghao: null,
          fuzeren: null,
          lianxidianhua: null,
          sfsh: null,
          shhf: null,
          thumbsupnum: null,
          crazilynum: null,
          clicktime: null,
          clicknum: null
        };
        // this.resetForm("form");
      },
      chenguoreset(){
        this.chengguoform = {
          id: null,
          addtime: null,
          xiangmumingcheng: null,
          xiangmutupian: null,
          keyanleibie: null,
          yanjiuchengguo: null,
          chengguoshuliang: null,
          jiexiangshijian: null,
          xuehao: null,
          xueshengxingming: null,
          zhuanye: null,
          keyanzhanghao: null,
          fuzeren: null,
          sfsh: null,
          shhf: null,
          thumbsupnum: null,
          crazilynum: null
        }
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.$refs["queryForm"].resetFields();
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        // this.single = selection.length!==1
        // this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        // this.reset();
        this.open = true;
        this.title = "添加科研项目";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getKeyanxiangmu(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改科研项目";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["formRef"].validate(valid => {
          if(valid){
            this.form.keyanzhanghao = this.$storage.get('adminName');
            this.$http({
              url: `/keyanxiangmu/add`,
              method: "post",
              data: this.form // 使用 params 而不是 data
          }).then(response => {
              this.open = false;
              this.getList();
          });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id ? [row.id] : this.ids;
        this.$confirm('是否确认删除科研项目编号为"' + ids + '"的数据项？').then(() => {
        // 用户点击了确定按钮，执行删除操作
        this.$http({
              url: `/keyanxiangmu/delete`,
              method: "post",
              data: ids
          }).then(response => {
              this.getList();
          });
      }).catch(() => {
        // 用户点击了取消按钮，执行其他操作或什么都不做
        this.ids = [];
        console.log('用户取消了删除操作');
      });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/keyanxiangmu/export', {
          ...this.queryParams
        }, `keyanxiangmu_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  